<template>
	<view>
		<cu-custom bgColor="bg-gradual-red" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{type==='add'?'新增':'修改'}}信息</block>
		</cu-custom>


		<form>


			<view class="cu-form-group margin-top">
				<view class="title">学生班级</view>
				<picker data-type="classes" @change="PickerChange" :value="classes" :range="picker">
					<view class="picker">
						{{ classes>-1 ? picker[classes] : '请选择类型'}}
					</view>
				</picker>
			</view>

			<view class="cu-form-group">
				<view class="title">学生姓名</view>
				<input v-model="name" name="name"></input>
			</view>

			<view class="cu-form-group">
				<view class="title">学生年龄</view>
				<input v-model="year" name="year" type="number"></input>
			</view>

			<view class="cu-form-group">
				<view class="title">学生生日</view>
				<picker mode="date" :value="birthday" start="2015-09-01" end="2020-09-01" data-type="birthday" @change="DateChange">
					<view class="picker">
						{{birthday}}
					</view>
				</picker>
			</view>

			<view class="cu-form-group">
				<view class="title">学生身高</view>
				<input v-model="height" type="number" name="input"></input>
			</view>

			<radio-group class="block" @change="RadioChange">
				<view class="cu-form-group">
					<!-- <input v-model="isMember" type="number" name="input"></input> -->
					<view class="title">是否Vip</view>
					<label class="action">
						<radio class='blue radio' :class="isMember?'checked':''" :checked="isMember?true:false" value="true"></radio>是
					</label>
					<label class="action">
						<radio class='red margin-left-sm' :class="!isMember?'checked':''" :checked="!isMember?true:false" value="false"></radio>
						否
					</label>
				</view>
			</radio-group>
			<view class="cu-form-group">
				<view class="title">学生等级</view>
				<picker @change="PickerChange" data-type="grades" :value="grades" :range="pickerGrades">
					<view class="picker">
						{{ grades>-1 ? pickerGrades[grades] : '请选择类型'}}
					</view>
				</picker>
			</view>

			<checkbox-group class="block" @change="CheckboxChange">
				<view class="cu-form-group">
					<view class="title">学生爱好</view>
					<label v-for="(item, index) in checkbox" :key="index" class="action">
						<checkbox :class="item.checked?'checked':''" :checked="item.checked?true:false" :value="item.value"></checkbox>
						{{item.value}}
					</label>
				</view>
			</checkbox-group>


			<!-- picture上传 -->
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<!-- picture显示 -->
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>

			<view class="cu-form-group align-start">
				<view class="title">学生描述</view>
				<textarea maxlength="-1" v-model="describe" placeholder="描述一下活动内容"></textarea>
			</view>
			<view class="padding flex flex-direction">
				<button @tap="toReg" v-if="type==='add'" class="cu-btn bg-red margin-tb-sm lg">新增</button>
				<button @tap="toReg" v-if="type==='update'" class="cu-btn bg-red margin-tb-sm lg">修改</button>
			</view>
		</form>
	</view>
</template>

<script>
	const {
		request,
		dateFormat
	} = require('@/utils/request.js');
	// import {
	// 	mapState,
	// } from 'vuex';
	import Vue from 'vue';
	export default {
		data() {
			return {
				classes: 0,
				picker: [
					'一年一班',
					'一年二班',
				],
				grades: 0,
				pickerGrades: [
					'合格',
					'不合格',
				],
				checkbox: [{
					value: '篮球',
					checked: false
				}, {
					value: '羽毛球',
					checked: false
				}, {
					value: '乒乓球',
					checked: false
				}],
				name: "",
				year: "",
				birthday: "",
				height: "",
				isMember: true,
				// hobby: "",
				imgList: [],
				describe: "",
				// 新增还是修改
				type: 'add',
				id: ''
			}
		},
		computed: {
			// ...mapState('userModule', ['userInfo']),
			today() {
				return dateFormat("YYYY-mm-dd", new Date())
			}
		},
		methods: {
			// 下拉
			PickerChange(e) {
				this[e.currentTarget.dataset.type] = e.detail.value;
			},
			// 时间
			DateChange(e) {
				this[e.currentTarget.dataset.type] = e.detail.value
			},
			// 单选
			RadioChange(e) {
				this.isMember = e.detail.value === "true" ? true : false;
			},
			// 多选
			CheckboxChange(e) {
				var items = this.checkbox,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					items[i].checked = false;
					for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
						if (items[i].value == values[j]) {
							items[i].checked = true;
							break
						}
					}
				}
			},
			// 图片
			ChooseImage() {
				let _this = this;
				uni.chooseImage({
					count: 4, //默认9
					//可以指定是原图还是压缩图，默认二者都有
					sizeType: ['original', 'compressed'],
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (_this.imgList.length != 0) {
							_this.imgList = _this.imgList.concat(res.tempFilePaths)
						} else {
							_this.imgList = res.tempFilePaths
						}
						_this.imgList.forEach((item, index) => {
							// console.log(item)
							// 逐条上传
							const uploadTask = uni.uploadFile({
								// url: "http://47.93.55.54:3300" + url,
								// url: 'http://localhost:3000/users/upload',
								url: 'http://47.93.55.54:3300/users/upload',
								filePath: item,
								name: "file",
								header: {
									// #ifdef MP-WEIXIN
									'Content-Type': 'multipart/from-data',
									// #endif
								},
								// formData: {
								// 	blob: item
								// },
								// file: item,

								success: (uploadFileRes) => {
									let urlimg = JSON.parse(uploadFileRes.data).result;
									// console.log(urlimg)
									if (urlimg) {
										_this.imgList.splice(index, 1, urlimg)
										// Vue.set([index], index, urlimg);
									} else {
										_this.imgList.splice(index, 1)
									}
								}
							});

							// uploadTask.onProgressUpdate((res) => {
							// 	console.log('上传进度' + res.progress);
							// 	console.log('已经上传的数据长度' + res.totalBytesSent);
							// 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
							// });
						})
					}
				});
			},
			ViewImage(e) {
				// console.log(this.imgList)
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '您好',
					content: '确定要删除吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			// 新增
			toReg() {
				if (typeof(this.year * 1) !== "number") return
				if (typeof(this.height * 1) !== "number") return
				let obj = {
					classes: this.classes,
					grades: this.grades,
					name: this.name,
					year: this.year * 1,
					height: this.height * 1,
					birthday: this.birthday,
					isMember: this.isMember,
					hobby: [...this.checkbox],
					imgList: [...this.imgList],
					describe: this.describe
				}
				// console.log(111)
				if (this.type === 'update') {
					// console.log(22)
					request({
						url: "/users/updateMsg",
						data: {
							...obj,
							_id: this.id
						},
						method: 'POST'
					}).then(res => {
						uni.navigateTo({
							url: '/pages/index/index'
						})
					})
				} else {
					// console.log(333)
					request({
						url: "/users/addMsg",
						data: obj,
						method: 'POST'
					}).then(res => {
						uni.navigateTo({
							url: '/pages/index/index'
						})
					})
				}
			}
		},
		mounted() {
			this.birthday = this.today;
			// this.end = this.today;
			if (this.type === 'update') {
				request({
					url: "/users/getMsgById",
					method: 'POST',
					data: {
						id: this.id
					}
				}).then(res => {
					let {
						classes,
						grades,
						checkbox,
						year,
						name,
						birthday,
						height,
						isMember,
						hobby,
						imgList,
						describe
					} = res.data;
					this.classes = classes;
					this.grades = grades;
					this.checkbox = checkbox;
					this.year = year;
					this.name = name;
					this.birthday = birthday;
					this.height = height;
					this.isMember = isMember;
					this.checkbox = hobby;
					this.imgList = imgList;
					this.describe = describe;
				})
			}
		},
		onLoad(options) {
			this.type = options.type;
			this.id = options.id;
		}
	}
</script>

<style>

</style>
